package com.platform.jpa.dao;

import com.platform.jpa.dao.other.Condition;
import com.platform.jpa.dao.other.Operation;
import com.platform.jpa.dao.other.Sort;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @param <T>  实体
 * @param <ID> 主键
 *             Iterable<Condition>  fields 条件
 *             Map<String, Boolean> sort 排序条件, true 升序, false 降序
 */
@NoRepositoryBean
public interface IBaseDao<T, ID> extends PagingAndSortingRepository<T, ID>, JpaSpecificationExecutor<T> {
    //-------------------增----------------------//
    <S extends T> int saveAllV2(Iterable<S> entities);
    boolean saveOrUpdate(Set<ID> existIds, List<T> entities);
    T saveOrUpdate(T entity);
    /*
    <S extends T> S save(S TEntity);
    <S extends T> Iterable<S> saveAll(Iterable<S> entities);
    */
    //-------------------删----------------------//
    /*
    void deleteById(ID id);
    void delete(T TEntity);
    void deleteAll(Iterable<? extends T> entities);
    void deleteAll();
    */
    int delete(Iterable<Condition> fields);

    int delete(String name, Operation op, Object value, boolean not);

    void deleteByIds(Iterable<ID> ids);
    int deleteByIdsV2(List<ID> ids);

    int deleteBySql(String sql);

    void deleteByIdWithFlag(ID id);

    //-------------------改----------------------//
    T update(T entity);

    Iterable<T> update(Iterable<T> entities);
    List<T> updateList(List<T> entities);

    int update(String name, Object value, Iterable<Condition> fields);

    int update(Map<String, Object> param, Iterable<Condition> fields);

    int updateBySql(String sql);

    int updateLockById(Map<String, Object> param, ID id, int lockIndex);
    int updateLockById(String fieldName, Object value, ID id, int lockIndex);

    int updateById(Map<String, Object> param, ID id);
    int update(String name, Object value, ID id);
    int updateById(String name, Object value, ID id);

    //-------------------查----------------------//
    /*
    boolean existsById(ID id);
    Iterable<T> findAll();
    Iterable<T> findAllById(Iterable<ID> ids);
    long count();
    Optional<T> findById(ID id);
    */
    Class<T> getTClass();

    T findTById(ID id);

    T findFirst(Iterable<Condition> fields, Map<String, Sort> sorts);

    T findFirst(Iterable<Condition> fields);

    T findFirstBySql(String sql);

    T findFirst(String fieldName, Operation op, Object value, boolean not);

    T findFirst(String fieldName, Operation op, Object value);

    List<T> findPage(int page, int row, Iterable<Condition> fields, Map<String, Sort> sorts);

    List<T> find(Iterable<Condition> fields, Map<String, Sort> sorts);

    List<T> find(Iterable<Condition> fields);

    List<T> find(String fieldName, Operation op, Object value, boolean not);

    List<T> find(String fieldName, Operation op, Object value);

    List<T> findPage(int page, int row, String fieldName, Operation op, Object value, boolean not);

    Iterable<T> findPageBySql(int page, int row, String sql);

    Iterable<T> findBySql(String sql);

    <M> Iterable<M> findPageBySql(int page, int row, String sql, Class<M> clsM);

    <M> Iterable<M> findBySql(String sql, Class<M> clsM);

    <M> M findFirstBySql(String sql, Class<M> clsM);

    List<Map<String, Object>> findMapBySql(String sql);

    Map<String, Object> findFisrtMapBySql(String sql);

    List<Map<String, Object>> findPageMapBySql(int page, int row, String sql);

    long count(Iterable<Condition> fields);

    @Deprecated
    long countBySql(String sql);

    long countBySqlV2(String sql);

    long count(String name, Operation op, Object value, boolean not);

    //-------------------其他----------------------//
    void flush();
}
